package com.example.controller;

import com.example.DTO.OrderItemDTO;
import com.example.DTO.OrderselectDTO;
import com.example.Ordermodule.Orderm;
import com.example.entity.StandardOrder;
import com.example.result.Result;
import com.example.entity.Order;
import com.example.service.OrderService;
import com.example.service.ProductService;
import com.example.service.StandardService;
import com.example.util.CreateOrderRequest;
import com.example.util.DeleteOrderRequest;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
@Tag(name = "订单模块", description = "订单模块的描述")
public class OrderController {

    @Autowired
    private OrderService orderservice;
    @Autowired
    private ProductService productService;
/*
  @GetMapping("/selectById/{buyerid}")
    @Operation(summary = "查看用户订单")
    public Result selectById(@PathVariable Integer buyerid) {
        List<OrderselectDTO> list = orderservice.selectbyid(buyerid);
        if (list != null && !list.isEmpty()) {
            return Result.success(list);
        } else {
            return Result.error("404", "暂无订单信息");
        }

    }
    @PostMapping("/addOrders")
    @Operation(summary = "购买商品后添加订单")
    public Result addOrders(@RequestBody Order order) {
        Integer isstock = orderservice.isstock(order.getProductid());
        if (isstock > 1) {
            orderservice.addOrders(order);
            orderservice.reducestock(order.getProductid());
            return Result.success();
        } else {
            orderservice.addOrders(order);
            orderservice.reducestock(order.getProductid());
            productService.freezeById(order.getProductid());
            return Result.success();
        }
    }*/
    /*已经结合冲突*/
    @PutMapping("/confirmation/{id}")
    @Operation(summary = "商家确认")
    public Result confirmation(@PathVariable Integer id) {
        orderservice.confirmation(id);
        return Result.success();
    }
    //已经更改
    @PutMapping("/stockup/{id}")
    @Operation(summary = "商家备货")
    public Result stockup(@PathVariable Integer id) {
        boolean isconfirmation = orderservice.isconfirmation(id);
        if (isconfirmation) {
            orderservice.stockup(id);
            return Result.success();
        } else {
            return Result.error("404", "请先确认商品，再进行备货");
        }
    }
    //已经更改
    @PutMapping("/sendorder/{id}")
    @Operation(summary = "商家发货")
    public Result sendorder(@PathVariable Integer id) {
        boolean isstockup = orderservice.isstockup(id);
        if (isstockup) {
            orderservice.sendorder(id);
            return Result.success();
        } else {
            return Result.error("404", "请先备货，再进行发货");
        }
    }
   //已更改
    @PutMapping("/completion/{id}")
    @Operation(summary = "买家确认收货")
    public Result completion(@PathVariable Integer id) {
        boolean issendorder = orderservice.issendorder(id);
        if (issendorder) {
            orderservice.completion(id);
            return Result.success();
        } else {
            return Result.error("404", "等待商家发货，再进行确认收货");
        }
    }
    //已更改
    @DeleteMapping("/buyerCancel/{id}")
    @Operation(summary = "买家取消订单")
    public Result buyercancel(@PathVariable Integer id) {
        boolean issendorder = orderservice.issendorder(id);
        if (!issendorder) {
            orderservice.cancelorder(id);
            return Result.success();
        } else {
            return Result.error("404", "商家已经发货，无法取消订单");
        }
    }
    //已更改
    @DeleteMapping("/sellerCancel/{id}")
    @Operation(summary = "卖家取消订单")
    public Result sellercancel(@PathVariable Integer id) {
        boolean iscompletion = orderservice.iscompletion(id);
        if (!iscompletion) {
            orderservice.cancelorder(id);
            return Result.success();
        } else {
            return Result.error("404", "交易已经完成，无法取消订单");
        }
    }

    //创建订单---new methon
    @Autowired
    private StandardService orderService;

    @PostMapping("/create")
    public Orderm createOrder(@RequestBody CreateOrderRequest request) {
        //使用StandaService创建订单
        return orderService.createOrder(request.getBuyerId(), request.getItems(), request.getAdressId());
    }

    //取消订单---即删除订单
    @DeleteMapping("/delete")
    public boolean deleteOrder(@RequestBody DeleteOrderRequest request) {
        return orderService.deleteOrder(request.getOrderId(), request.getProductIds());
    }

    //获取订单相关信息
    @GetMapping("/apid/{buyerId}")
    public List<Map<String, Object>> getPaidOrders(@PathVariable Integer buyerId, @RequestParam(required = false) String status) {
        return orderService.getPaidOrders(buyerId, status);
    }


 /*   @GetMapping("/selectOrders")
    @Operation(summary = "查询所有订单")
    public Result selectOrders() {
        List<OrderselectDTO> list = orderservice.selectorders();
        return Result.success(list);
    }*/

    //后台卖家获取所有已支付的订单
    @GetMapping("/selectOrders")
    public Result getAllPaidOrders(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize
    ) {
        List<OrderItemDTO> list = orderService.getPaidOrdersByPage(pageNum, pageSize);
        return Result.success(list);
    }
}





